package dp.test198;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/23 11:47
 */
public class Solution {

    public int rob(int[] nums) {
        if (nums.length == 0) return 0;
        int[] dp = new int[nums.length + 1];
        for (int i = 0; i < nums.length; i++) {
            if (i == 0) {
                dp[i + 1] = nums[i];
            } else {
                dp[i + 1] = Math.max(dp[i], dp[i - 1] + nums[i]);
            }
        }
        return dp[nums.length];
    }

    // 优化动态规划
    public int rob2(int[] nums) {
        if(nums == null || nums.length == 0) return 0;
        int n = nums.length;
        int c1 = 0, c2 = nums[0];
        int ans = nums[0];
        for(int i = 2; i <= n; i++){
            ans = Math.max(c1 + nums[i - 1], c2);
            c1 = c2;
            c2 = ans;
        }
        return ans;
    }
}
